package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.course.CourseDTO;
import com.qk.entity.Course;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author:Shuna
 * @Date: 2025/10/09 10:07
 * @Description:
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public PageResult<Course> page(CourseDTO courseDTO) {
        PageHelper.startPage(courseDTO.getPage(), courseDTO.getPageSize());
        List<Course> courseList = courseMapper.selectByPage(courseDTO);
        Page<Course> page = (Page<Course>)  courseList;
        return PageResult.<Course>builder().total(Long.valueOf(page.getTotal()).intValue()).rows(page.getResult()).build();
    }

    @Override
    public void add(Course course) {
        boolean hasNull = BeanUtil.hasNullField(course, "id","description","updateTime","createTime");
        if (hasNull) {
            throw new RuntimeException("参数错误");
        }
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.insert(course);
    }

    @Override
    public void deleteById(Integer id) {
        courseMapper.deleteById(id);
    }

    @Override
    public Course getById(Integer id) {
        return courseMapper.selectById(id);
    }

    @Override
    public void update(Course course) {
        boolean hasNull = BeanUtil.hasNullField(course, "description","updateTime","createTime");
        if (hasNull) {
            throw new RuntimeException("参数错误");
        }
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);
    }

    @Override
    public List<Course> getAll() {
        return courseMapper.selectAll();
    }

    @Override
    public List<Course> getAllBySubject(Integer subject) {
        return courseMapper.selectAllBySubject(subject);
    }


}
